package com.woniu.service.Impl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.woniu.dao.CreditOrderDao;
import com.woniu.dao.CreditRefundDao;
import com.woniu.entity.CreOrder;
import com.woniu.entity.CreRefund;
import com.woniu.service.CreditOrderService;
import com.woniu.tool.FormatDecimal;
import com.woniu.tool.RefundUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Service业务层处理
 *
 * @author 陈泓霖
 * @date 2022-09-21
 */
@Service
public class CreditOrderServiceImpl implements CreditOrderService {
    @Autowired
    private CreditOrderDao creditOrderDao;

    @Autowired
    private CreditRefundDao creditRefundDao;

//    @Autowired
//    private RefundUtil refundUtil;

    /**
     * 查询
     *
     * @param orderId 主键
     * @return
     */
    @Override
    public CreOrder selectCreOrderByOrderId(long orderId) {
        return creditOrderDao.selectCreOrderByOrderId(orderId);
    }

    /**
     * 查询列表
     *
     * @param creOrder
     * @return
     */
    @Override
    public List<CreOrder> selectCreOrderList(CreOrder creOrder) {
        List<CreOrder> listCreOrder = creditOrderDao.selectCreOrderList(creOrder);
        return listCreOrder;
    }

    /**
     * 新增
     *
     * @param creOrder
     * @return 结果
     */
    @Override
    public int insertCreOrder(CreOrder creOrder) {
        creOrder.setCreateTime(DateUtils.getNowDate());
        return creditOrderDao.insertCreOrder(creOrder);
    }

    /**
     * 修改
     *
     * @param creOrder
     * @return 结果
     */
    @Override
    public int updateCreOrder(CreOrder creOrder) {
        creOrder.setCreateTime(DateUtils.getNowDate());
        return creditOrderDao.updateCreOrder(creOrder);
    }

    /**
     * 批量删除
     *
     * @param orderIds 需要删除的主键
     * @return 结果
     */
    @Override
    public int deleteCreOrderByOrderIds(Long[] orderIds) {
        return creditOrderDao.deleteCreOrderByOrderIds(orderIds);
    }

    /**
     * 删除信息
     *
     * @param orderId 主键
     * @return 结果
     */
    @Override
    public int deleteCreOrderByOrderId(Long orderId) {
        return creditOrderDao.deleteCreOrderByOrderId(orderId);
    }


    @Override
    public List<CreOrder> userOrderList(CreOrder creOrder) {
        int userId = Integer.parseInt(SecurityUtils.getUserId().toString());
        creOrder.setUserId(userId);
        List<CreOrder> creOrders = creditOrderDao.userOrderList(creOrder);
        for (CreOrder order : creOrders) {
            Integer orderId = order.getOrderId();
            CreRefund creRefund = creditRefundDao.getRefundByOrderId(orderId);
            order.setCreRefund(creRefund);
        }
        return creOrders;
    }

    /**
     * 等额本息	0
     * 等本等息	1
     *
     * @param orderId
     * @return
     */
    @Override
    public AjaxResult getRefund(int orderId) {
        CreRefund creRefund = creditRefundDao.getRefund(orderId);
        CreOrder creOrder = creditOrderDao.selectCreOrderByOrderId(orderId);
        String paymentMethod = creRefund.getPaymentMethod();
        double applymoney = creOrder.getApplymoney();
        double loanTerm = Double.parseDouble(creRefund.getLoanTerm());
        double monthRate = creRefund.getMonthRate();
        double consultingRate = creRefund.getConsultingRate();
        double marginRate = creRefund.getMarginRate();
//        等额本息
        if (paymentMethod.equals("0")) {
            String[] strings = RefundUtil.calculateEqualPrincipalAndInterest(applymoney, loanTerm, monthRate, consultingRate, marginRate);
            creRefund.setShouldTotal(Double.valueOf(strings[0]));
            creRefund.setMonthlyPayment(Double.parseDouble(strings[3]));
            creRefund.setGrossInterest(Double.valueOf(strings[2]));
        } else if (paymentMethod.equals("1")) {
            /*
              等额本金
             */
            String[] strings = RefundUtil.calculateEqualPrincipal(applymoney, (int) loanTerm, monthRate, consultingRate, marginRate);
            creRefund.setShouldTotal(Double.valueOf(strings[0]));
//            每月应还本金
            double m = Double.parseDouble(strings[1]) / Double.parseDouble(creRefund.getLoanTerm());
            if (creRefund.getRefundCount() == Integer.parseInt(creRefund.getLoanTerm())) {
//                首月利息
                double v = Double.parseDouble(strings[1]) * creRefund.getMonthRate() / 100;
                creRefund.setGrossInterest(Double.parseDouble(FormatDecimal.format(v)));
            }
//            本次应还
            double i = m + (creRefund.getGrossInterest() - ((Integer.parseInt(creRefund.getLoanTerm()) - creRefund.getRefundCount()) * Double.parseDouble(strings[4])));
            creRefund.setMonthlyPayment(Double.parseDouble(FormatDecimal.format(i)));
        }
        return AjaxResult.success(creRefund);
    }

    /**
     * 增加订单
     *
     * @param creOrder
     */
    @Override
    public int addOrder(CreOrder creOrder) {
        return creditOrderDao.addOrder(creOrder);
    }

    @Override
    public AjaxResult getDisRefundById(int orderId) {
        CreRefund creRefund = creditRefundDao.getRefund(orderId);
        CreOrder creOrder = creditOrderDao.selectCreOrderByOrderId(orderId);
        String paymentMethod = creRefund.getPaymentMethod();
        double applymoney = creOrder.getApplymoney();
        double loanTerm = Double.parseDouble(creRefund.getLoanTerm());
        double monthRate = creRefund.getMonthRate();
        double consultingRate = creRefund.getConsultingRate();
        double marginRate = creRefund.getMarginRate();
        // 还款次数
        int refundCount = Integer.parseInt(creRefund.getLoanTerm()) - creRefund.getRefundCount();
//        等额本息
        if (paymentMethod.equals("0")) {
            String[] strings = RefundUtil.calculateEqualPrincipalAndInterest(applymoney, (int) loanTerm, refundCount, monthRate, consultingRate, marginRate);
            // 还款总额
            creRefund.setShouldTotal(Double.valueOf(strings[0]));
            // 还款次数清零
            creRefund.setRefundCount(0);
            // 还款总额
            creRefund.setShouldTotal(Double.valueOf(strings[0]));
            // 本次一次性还款金额
            creRefund.setMonthlyPayment(Double.parseDouble(strings[7]));
        } else if (paymentMethod.equals("1")) {
            /*
              等额本金
             */
            String[] strings = RefundUtil.calculateEqualPrincipal(applymoney, (int) loanTerm, refundCount, monthRate, consultingRate, marginRate);
            // 还款总额
            creRefund.setShouldTotal(Double.valueOf(strings[0]));
            // 还款次数清零
            creRefund.setRefundCount(0);
            // 本次一次性还款金额
            creRefund.setMonthlyPayment(Double.parseDouble(strings[8]));
        }
        return AjaxResult.success(creRefund);
    }

    @Override
    public CreOrder selectOrderByOrderId(Integer orderId) {


        return creditOrderDao.selectOrderByOrderId(orderId);
    }
}
